<?php
class ext_barang_masuk_model extends MY_Model {
	var $nama_tabel = 'ext_barang_masuk';
	
	function GetJSON() {
		$ls_sql	= "	SELECT	a.id_barang_masuk_pk, a.id_po_fk, a.ket_barang_masuk, 
					TO_CHAR(a.tgl_barang_masuk,'DD/MM/YYYY') tgl_barang_masuk
					FROM	".$this->nama_tabel." a
					WHERE	(UPPER(id_barang_masuk_pk) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(id_po_fk) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(NVL(ket_barang_masuk,'')) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.id_barang_masuk_pk' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		//echo $ls_sql;
		return json_encode( $this->paging($ls_sql) );
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_barang_masuk_pk' => $arr_rule['id_barang_masuk_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GenerateId() {
		$sql = "select nvl(max(id_barang_masuk_pk), 0)+1 max 
				from ext_barang_masuk
				";
		$data = $this->db->query($sql)->row_array();

		return intval($data['max']);
	}

	function GetSingleView($id_barang_masuk) {
		$sql = "select id_barang_masuk_pk, id_po_fk, TO_CHAR(tgl_barang_masuk,'DD/MM/YYYY') tgl_barang_masuk, 
				ket_barang_masuk
				from ext_barang_masuk
				where id_barang_masuk_pk = '".strtoupper($id_barang_masuk)."'
				";

		$result = $this->db->query($sql);


		if($result->num_rows() == 1) {
			return $result->row_array();
		} else {
			return NULL;
		}

	}

}
?>